#include "user_fs.h"

#define TAG "user_fs"

static void user_fs_read_and_print_file(void);
static void user_fs_test(void);

// LittleFS 初始化函数
esp_err_t user_fs_init(void) {
    ESP_LOGI(TAG, "Initializing LittleFS");

    esp_vfs_littlefs_conf_t conf = {
        .base_path = POINT_PATH,
        .partition_label = "storage",
        .format_if_mount_failed = true,
        .dont_mount = false,
    };

    // 初始化 LittleFS
    esp_err_t ret = esp_vfs_littlefs_register(&conf);
    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        } else if (ret == ESP_ERR_NOT_FOUND) {
            ESP_LOGE(TAG, "Failed to find LittleFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
        }
        return ret;
    }

    // 获取文件系统信息
    size_t total = 0, used = 0;
    ret = esp_littlefs_info(conf.partition_label, &total, &used);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
        esp_littlefs_format(conf.partition_label);
    } else {
        ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }

    // user_fs_test();

    return ESP_OK;
}

// LittleFS 取消挂载
void user_fs_deinit(void) {
    ESP_LOGI(TAG, "Unmounting LittleFS");
    esp_vfs_littlefs_unregister("storage");
}

#define BUFFER_SIZE 1024
uint8_t buffer[BUFFER_SIZE];
static void user_fs_test(void) {
    FILE *f_read, *f_write;
    f_read = fopen("/littlefs/input.opus", "rb");
    if (f_read == NULL) {
        ESP_LOGE(TAG, "Failed to open input file for reading");
        return;
    }
    f_write = fopen("/littlefs/output.opus", "wb");
    if (f_write == NULL) {
        ESP_LOGE(TAG, "Failed to open output file for writing");
        fclose(f_read);
        return;
    }
    while (1) {
        size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, f_read);
        if (bytes_read == 0) {
            break; // EOF
        }
        size_t bytes_written = fwrite(buffer, 1, bytes_read, f_write);
        if (bytes_written != bytes_read) {
            ESP_LOGE(TAG, "Write failed: %zu bytes written", bytes_written);
            break;
        }
    }
    fclose(f_read);
    fclose(f_write);
}

// // 测试 1MB 数据的读写速度
// void user_fs_test_speed(void) {
//     ESP_LOGI(TAG, "Starting speed test...");

//     // 分配 1MB 的缓冲区
//     uint8_t *buffer = malloc(TEST_SIZE);
//     if (!buffer) {
//         ESP_LOGE(TAG, "Failed to allocate buffer");
//         return;
//     }
//     memset(buffer, 0xAA, TEST_SIZE); // 填充数据

//     // 记录写入开始时间
//     int64_t start_time = esp_timer_get_time();

//     // 打开文件进行写入
//     FILE *f = fopen(TEST_FILE, "wb");
//     if (!f) {
//         ESP_LOGE(TAG, "Failed to open file for writing");
//         free(buffer);
//         return;
//     }

//     size_t written = fwrite(buffer, 1, TEST_SIZE, f);
//     fclose(f);

//     // 记录写入结束时间
//     int64_t end_time = esp_timer_get_time();
//     if (written == TEST_SIZE) {
//         ESP_LOGI(TAG, "Write 1MB done in %lld ms", (end_time - start_time) / 1000);
//     } else {
//         ESP_LOGE(TAG, "Write failed! Only wrote %d bytes", written);
//     }

//     // 清空缓冲区
//     memset(buffer, 0, TEST_SIZE);

//     // 记录读取开始时间
//     start_time = esp_timer_get_time();

//     // 打开文件进行读取
//     f = fopen(TEST_FILE, "rb");
//     if (!f) {
//         ESP_LOGE(TAG, "Failed to open file for reading");
//         free(buffer);
//         return;
//     }

//     size_t read = fread(buffer, 1, TEST_SIZE, f);
//     fclose(f);

//     // 记录读取结束时间
//     end_time = esp_timer_get_time();
//     if (read == TEST_SIZE) {
//         ESP_LOGI(TAG, "Read 1MB done in %lld ms", (end_time - start_time) / 1000);
//     } else {
//         ESP_LOGE(TAG, "Read failed! Only read %d bytes", read);
//     }

//     // 清理
//     free(buffer);
//     ESP_LOGI(TAG, "Speed test complete.");
// }